package safe_map

import "sync"

type SafeMap interface {
	Set(key string, value interface{})
	Get(key string) interface{}
	Gets(key string) (interface{}, bool)
}

type ConfigSafeMap struct {
	Data map[string]interface{}
	mu   sync.RWMutex // 读写锁（读多写少时性能更好）
}

func (s *ConfigSafeMap) Set(key string, value interface{}) {
	s.mu.Lock()
	defer s.mu.Unlock()
	if s.Data == nil {
		s.Data = make(map[string]interface{})
	}
	s.Data[key] = value
}

func (s *ConfigSafeMap) Get(key string) interface{} {
	s.mu.RLock()
	defer s.mu.RUnlock()
	val, _ := s.Data[key]
	return val
}

func (s *ConfigSafeMap) Gets(key string) (interface{}, bool) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	val, ok := s.Data[key]
	return val, ok
}
